﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.WorkAttendance
{
    /// <summary>
    /// SearchData 的摘要说明。
    /// </summary>
    public partial class SearchDataPage : Page
    {
        protected DataView dvw;
        protected DataView dvw1;
        protected DataView dvwAttGrid;

        protected int GetGridData(int staffid, int type)
        {
            //type:0 正常 1：迟到 2：早退 3：未考勤 4：总考勤天数
            switch (type)
            {
                case 0:
                    dvw.RowFilter = "OnDuty_Status = false and OffDuty_Status = false and staff_id=" + staffid;
                    break;
                case 1:
                    dvw.RowFilter = "OnDuty_Status = true and staff_id=" + staffid;
                    break;
                case 2:
                    dvw.RowFilter = "OffDuty_Status = true and staff_id=" + staffid;
                    break;
                case 3:
                    dvw.RowFilter = "staff_id=" + staffid;
                    return dvw1.Count - dvw.Count;
                case 4:
                    break;
            }
            return dvw.Count;
        }

        protected override void OnInit(EventArgs e)
        {
            ddlsearchbound.SelectedIndexChanged += ddlsearchbound_SelectedIndexChanged;
            btnsearch.Click += btnsearch_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                /*
                 * The reporting feature is temporarily disabled!
                 */
                // btn_Report.Style["display"] = "none";

                //绑定两个select框
                DataBindObject(ddldepartments);
                DataBindObject(lbstaffs);
            }

            base.OnLoad(e);
        }

        private void DataBindObject(object sender)
        {
            var db = new Database();
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
            {
                using (SqlDataReader dr = db.RunDataReader("dbo.sproc_Ext_GetPosition"))
                {
                    ((DropDownList)sender).DataSource = dr;
                    ((DropDownList)sender).DataTextField = "Position_Name";
                    ((DropDownList)sender).DataValueField = "Position_ID";
                    ((DropDownList)sender).DataBind();
                }
            }
            else if (sender.GetType().ToString() == "System.Web.UI.WebControls.ListBox")
            {
                ((ListBox)sender).DataSource = Staff.GetAllStaff(false);
                ((ListBox)sender).DataTextField = "RealName";
                ((ListBox)sender).DataValueField = "ID";
                ((ListBox)sender).DataBind();
            }
        }

        private void ddlsearchbound_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((DropDownList)sender).SelectedItem.Value == "0")
            {
                ddldepartments.Visible = false;
                lbstaffs.Visible = false;
                lbstaffs.Visible = false;
            }
            else if (((DropDownList)sender).SelectedItem.Value == "1")
            {
                ddldepartments.Visible = true;
                lbstaffs.Visible = false;
            }
            else if (((DropDownList)sender).SelectedItem.Value == "2")
            {
                lbstaffs.Visible = true;
                ddldepartments.Visible = false;
            }
        }

        [Obsolete(@"
1. Call Staff.GetStaffInPosition instead of running sproc_GetStaffInPosition_V2;
2. Call Staff.GetAllStaff instead of running sproc_GetAllStaff")]
        private void btnsearch_Click(object sender, EventArgs e)
        {
            /*
             * The reporting feature is temporarily disabled!
             */
            // btn_Report.Style["display"] = "";

            var ds = new DataSet();
            var db = new Database();
            string idtype;
            switch (ddlsearchbound.SelectedIndex)
            {
                case 0:
                    idtype = "company";
                    break;
                case 1:
                    idtype = "Position";
                    break;
                default:
                    idtype = "staff";
                    break;
            }

            var beginTime = txtbegintime.SelectedDate;
            var endTime = txtendtime.SelectedDate;
            List<int> staffList = null;
            int deptId = -1;
            SqlDataReader dr;

            switch (idtype)
            {
                case "staff":
                    {
                        staffList = new List<int>();
                        foreach (ListItem listItem in lbstaffs.Items)
                        {
                            if (listItem.Selected)
                            {
                                staffList.Add(int.Parse(listItem.Value));
                            }
                        }

                        dr = WA_Duty.GetStaffAttendanceData(beginTime, endTime, staffList);
                    }
                    break;
                case "Position":
                    {
                        deptId = int.Parse(ddldepartments.SelectedItem.Value);
                        dr = WA_Duty.GetPositionAttendanceData(beginTime, endTime, new[] { deptId });
                    }
                    break;
                default:
                    dr = WA_Duty.GetCompanyAttendanceData(beginTime, endTime);
                    break;
            }
            using (dr)
            {
                DataTable datatable = Tools.ConvertDataReaderToDataTable(dr);
                ds.Tables.Add(datatable);
                dvw = ds.Tables[0].DefaultView;
            }

            ////得到考勤数据
            //prams[0] = Database.MakeInParam("@begintime", SqlDbType.DateTime, 8, txtbegintime.Text);
            //prams[1] = Database.MakeInParam("@endtime", SqlDbType.DateTime, 8, txtendtime.Text);
            //prams[2] = Database.MakeInParam("@ids", SqlDbType.NVarChar, 1000, ids);
            //prams[3] = Database.MakeInParam("@idtype", SqlDbType.NVarChar, 50, idtype);
            //using (SqlDataReader dr = db.RunDataReader("dbo.sproc_WA_GetAttendanceData", prams))
            //{
            //    DataTable datatable = Tools.ConvertDataReaderToDataTable(dr);
            //    ds.Tables.Add(datatable);
            //    dvw = ds.Tables[0].DefaultView;
            //}

            //得到人员名单
            if (idtype == "staff")
            {
                SqlParameter[] prams1 = { Database.MakeInParam("@ids", SqlDbType.NVarChar, 1000, Utility.JoinArray(staffList, ',')) };
                using (SqlDataReader dr1 = db.RunDataReader("dbo.sproc_WA_GetSelectedStaffFromID", prams1))
                {
                    DataTable datatable1 = Tools.ConvertDataReaderToDataTable(dr1);
                    ds.Tables.Add(datatable1);
                    dvwAttGrid = ds.Tables[1].DefaultView;
                }
            }
            else if (idtype == "Position")
            {
                SqlParameter[] prams1 = {
                        Database.MakeInParam("@Position_id", SqlDbType.Int, 4, deptId),
                        Database.MakeInParam("@Dimission", SqlDbType.Bit, 1, 0)
                    };
                using (SqlDataReader dr1 = db.RunDataReader("dbo.sproc_GetStaffInPosition_V2", prams1))
                {
                    DataTable datatable1 = Tools.ConvertDataReaderToDataTable(dr1);
                    ds.Tables.Add(datatable1);
                    dvwAttGrid = ds.Tables[1].DefaultView;
                }
            }
            else if (idtype == "company")
            {
                SqlParameter[] prams1 = {
                        Database.MakeInParam("@StaffType", SqlDbType.Int, 4, 0)
                    };
                using (SqlDataReader dr1 = db.RunDataReader("dbo.sproc_GetAllStaff", prams1))
                {
                    DataTable datatable1 = Tools.ConvertDataReaderToDataTable(dr1);
                    ds.Tables.Add(datatable1);
                    dvwAttGrid = ds.Tables[1].DefaultView;
                }
            }

            SqlParameter[] prams2 = {
                    Database.MakeInParam("@begintime", SqlDbType.DateTime, 8, txtbegintime.Text),
                    Database.MakeInParam("@endtime", SqlDbType.DateTime, 8, txtendtime.Text)
                };
            using (SqlDataReader dr2 = db.RunDataReader("dbo.sproc_WA_GetDutyDay", prams2))
            {
                DataTable datatable2 = Tools.ConvertDataReaderToDataTable(dr2);
                ds.Tables.Add(datatable2);
                dvw1 = ds.Tables[2].DefaultView;
            }

            AttendanceGrid.DataSource = dvwAttGrid;
            AttendanceGrid.DataBind();

            //缓存数据
            Cache["WA_Duty"] = ds;

            /*
             * The reporting feature is temporarily disabled!
             */
            // btn_Report.Attributes["onclick"] = "window.open('Report/DutyReport.aspx?idtype=" + idtype + "&ids=" + ids + "&begintime=" + txtbegintime.Text + "&endtime=" + txtendtime.Text + "')";
        }
    }
}